package com.todod.service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.ProjectCatalogDto;
import com.todod.mapper.ProjectCatalogMapper;
import com.todod.model.ProjectCatalog;

@Service
@Transactional
public class ProjectCatalogService {

	@Autowired
	private ProjectCatalogMapper projectCatalogMapper;

	/**
	 * 
	 * @param companyId
	 * @param xiangmuId
	 * @return
	 */
	public List<ProjectCatalog> getListByParentId(Long companyId, Long xiangmuId, Long _userId) {

		QueryWrapper<ProjectCatalog> queryWrapper = new QueryWrapper<>();
		if (xiangmuId != null) {
			queryWrapper.eq("xft_xiangmu_id", xiangmuId);
		}

		queryWrapper.eq("xft_department_id", companyId);
		queryWrapper.eq("xft_createuser_id", _userId);
		queryWrapper.eq("xft_flag", "U");
		queryWrapper.orderByAsc("xft_sort_num");

		List<ProjectCatalog> list = projectCatalogMapper.selectList(queryWrapper);

		return list;
	}

	public List<ProjectCatalog> getListByFlag(String flag, Long xiangmuId) {

		QueryWrapper<ProjectCatalog> queryWrapper = new QueryWrapper<>();
		if (xiangmuId != null) {
			queryWrapper.eq("xft_xiangmu_id", xiangmuId);
		}
		queryWrapper.eq("xft_flag", flag);
		queryWrapper.orderByAsc("xft_sort_num");

		List<ProjectCatalog> list = projectCatalogMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * 
	 * @param _userId
	 * @param projectCatalog
	 * @param value
	 * @param value2
	 */

	public void saveData(Long _userId, ProjectCatalog projectCatalog) {

		projectCatalog.setId(SnowflakeIdGenerator.getId());
		projectCatalog.setState("1");

		projectCatalog.setCreatetimeTi(LocalDateTime.now());
		projectCatalog.setCreateuserId(_userId);
		projectCatalogMapper.insert(projectCatalog);
	}

	/**
	 * 
	 * @param _userId
	 * @param projectCatalog
	 * @param value
	 * @param value2
	 */
	public void editData(Long _userId, ProjectCatalog projectCatalog) {

		ProjectCatalog projectCatalog1 = projectCatalogMapper.selectById(projectCatalog.getId());

		projectCatalog.setCreatetimeTi(projectCatalog1.getCreatetimeTi());
		projectCatalog.setCreateuserId(projectCatalog1.getCreateuserId());
		projectCatalog.setModifytimeTi(LocalDateTime.now());
		projectCatalog.setModifyuserId(_userId);
		projectCatalogMapper.updateById(projectCatalog);
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public Map<String, Object> getInfo(Long code) {
		Map<String, Object> map = new HashMap<>();
		ProjectCatalogDto projectCatalog = projectCatalogMapper.getInfo(code);

		map.put("projectCatalog", projectCatalog);
		return map;
	}

	/**
	 * @Title: getSortNumByType
	 * @Description:根据项目类型获得项目编号
	 * @author: dyl
	 * @date 2020-03-15
	 * @param type
	 * @param departid
	 * @return
	 */
	public ProjectCatalog getSortNumByType(String type, Long departid) {
		QueryWrapper<ProjectCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("xg_type", type);
		queryWrapper.eq("xg_department_id", departid);

		ProjectCatalog projectCatalog = projectCatalogMapper.selectOne(queryWrapper);

		return projectCatalog;
	}

	/**
	 * 
	 * @param _userId
	 * @param string
	 */
	public void complete(Long _userId, String id) {
		ProjectCatalog projectCatalog = projectCatalogMapper.selectById(Long.valueOf(id));

		projectCatalog.setState("5");
		projectCatalog.setModifytimeTi(LocalDateTime.now());
		projectCatalog.setModifyuserId(_userId);
		projectCatalogMapper.updateById(projectCatalog);
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public ProjectCatalog getCatalogByCode(Long code) {
		QueryWrapper<ProjectCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("xft_id", code);

		ProjectCatalog projectCatalog = projectCatalogMapper.selectOne(queryWrapper);

		return projectCatalog;
	}

}
